import pygame
import sys
import os
import math
from config import *

class StartScreen:
    def __init__(self, screen):
        self.screen = screen
        self.animation_time = 0
        
        # 尝试使用系统中文字体
        self.font_large = self.get_chinese_font(72)
        self.font_medium = self.get_chinese_font(36)
        self.font_small = self.get_chinese_font(24)
        
        # 按钮属性
        self.button_width = 200
        self.button_height = 50
        self.button_x = SCREEN_WIDTH // 2 - self.button_width // 2
        self.start_button_y = SCREEN_HEIGHT // 2
        self.quit_button_y = SCREEN_HEIGHT // 2 + 100
        
        # 按钮矩形
        self.start_button_rect = pygame.Rect(self.button_x, self.start_button_y, self.button_width, self.button_height)
        self.quit_button_rect = pygame.Rect(self.button_x, self.quit_button_y, self.button_width, self.button_height)
        
        # 按钮颜色
        self.button_color = (70, 130, 180)  # 钢蓝色
        self.button_hover_color = (100, 149, 237)  # 玉米花蓝色
        self.button_text_color = WHITE
        
        # 标题
        self.title_text = self.font_large.render("战斗闯关游戏", True, WHITE)
        self.title_rect = self.title_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 4))
        
        # 副标题
        self.subtitle_text = self.font_small.render("使用方向键移动，空格键攻击", True, WHITE)
        self.subtitle_rect = self.subtitle_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 4 + 80))
        
    def get_chinese_font(self, size):
        # 尝试几种常见的中文字体
        font_names = [
            "simhei.ttf",  # 黑体
            "simsun.ttc",  # 宋体
            "msyh.ttc",    # 微软雅黑
            "arial unicode ms.ttf"  # Arial Unicode
        ]
        
        # 首先尝试使用系统字体
        try:
            # 在Windows系统中查找字体
            font_paths = [
                "C:/Windows/Fonts/",
                "/System/Library/Fonts/",  # macOS
                "/usr/share/fonts/"        # Linux
            ]
            
            for font_name in font_names:
                for font_path in font_paths:
                    full_path = os.path.join(font_path, font_name)
                    if os.path.exists(full_path):
                        return pygame.font.Font(full_path, size)
        except:
            pass
        
        # 如果找不到系统字体，使用默认字体
        return pygame.font.Font(None, size)
        
    def handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.start_button_rect.collidepoint(event.pos):
                    return "start_game"
                elif self.quit_button_rect.collidepoint(event.pos):
                    pygame.quit()
                    sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    return "start_game"
                elif event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
        return None
    
    def update(self):
        # 更新动画时间
        self.animation_time += 0.1
    
    def draw_button(self, rect, text, hover=False):
        # 绘制按钮
        color = self.button_hover_color if hover else self.button_color
        pygame.draw.rect(self.screen, color, rect, border_radius=15)
        pygame.draw.rect(self.screen, WHITE, rect, 3, border_radius=15)
        
        # 绘制按钮文本
        button_text = self.font_medium.render(text, True, self.button_text_color)
        text_rect = button_text.get_rect(center=rect.center)
        self.screen.blit(button_text, text_rect)
    
    def draw_forest_elements(self):
        # 绘制森林背景元素
        for i in range(20):
            # 使用动画时间创建波动效果
            offset = math.sin(self.animation_time + i) * 5
            x = i * 50
            y = SCREEN_HEIGHT - 60 + offset
            
            # 绘制草丛
            pygame.draw.circle(self.screen, GREEN, (x, y), 15)
        
        # 绘制一些树木
        for i in range(5):
            x = 100 + i * 150
            y = SCREEN_HEIGHT - 100
            
            # 树干
            pygame.draw.rect(self.screen, BROWN, (x, y, 15, 60))
            # 树冠
            pygame.draw.circle(self.screen, DARK_GREEN, (x + 7, y), 30)
    
    def draw_animated_title(self):
        # 绘制动画标题
        title_y_offset = math.sin(self.animation_time * 2) * 10
        animated_title_rect = self.title_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 4 + title_y_offset))
        self.screen.blit(self.title_text, animated_title_rect)
        
        # 绘制标题阴影效果
        shadow_offset = 3
        shadow_title_rect = self.title_text.get_rect(center=(SCREEN_WIDTH // 2 + shadow_offset, 
                                                           SCREEN_HEIGHT // 4 + title_y_offset + shadow_offset))
        shadow_title = self.font_large.render("战斗闯关游戏", True, (100, 100, 100))
        self.screen.blit(shadow_title, shadow_title_rect)
    
    def draw(self):
        # 绘制渐变背景
        for y in range(SCREEN_HEIGHT):
            # 创建从深蓝到浅蓝的渐变
            color_value = max(0, min(255, 100 + (y / SCREEN_HEIGHT) * 155))
            color = (0, color_value // 2, color_value)  # 蓝色渐变
            pygame.draw.line(self.screen, color, (0, y), (SCREEN_WIDTH, y))
        
        # 绘制森林背景元素
        self.draw_forest_elements()
        
        # 绘制动画标题
        self.draw_animated_title()
        
        # 绘制副标题（也添加动画效果）
        subtitle_y_offset = math.sin(self.animation_time * 2 + 0.5) * 5
        animated_subtitle_rect = self.subtitle_text.get_rect(center=(SCREEN_WIDTH // 2, 
                                                                   SCREEN_HEIGHT // 4 + 80 + subtitle_y_offset))
        self.screen.blit(self.subtitle_text, animated_subtitle_rect)
        
        # 检查鼠标是否悬停在按钮上
        mouse_pos = pygame.mouse.get_pos()
        start_hover = self.start_button_rect.collidepoint(mouse_pos)
        quit_hover = self.quit_button_rect.collidepoint(mouse_pos)
        
        # 绘制按钮
        self.draw_button(self.start_button_rect, "开始游戏", start_hover)
        self.draw_button(self.quit_button_rect, "退出游戏", quit_hover)
        
        # 绘制版权信息
        copyright_text = self.font_small.render("© 2025 战斗闯关游戏", True, WHITE)
        copyright_rect = copyright_text.get_rect(bottomright=(SCREEN_WIDTH - 10, SCREEN_HEIGHT - 10))
        self.screen.blit(copyright_text, copyright_rect)
        
        # 绘制操作提示
        controls_text = self.font_small.render("使用 A/D 键移动，空格键跳跃，J 键攻击", True, WHITE)
        controls_rect = controls_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 40))
        self.screen.blit(controls_text, controls_rect)